import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoadingDialog extends Dialog {


  final String title; // 内容文本
  final bool isCancel=true; // 是否点击外部或者返回键关闭Dialog
  Color? color; // 内容背景颜色
  BorderRadiusGeometry borderRadius=BorderRadius.circular(2); // 内容倒角半径
  EdgeInsetsGeometry padding=EdgeInsets.fromLTRB(20, 15, 20, 15); // 内容内边距
  final double indicatorRadius=20; // 菊花半径
  final Color textColor=Colors.black45; // 文本颜色
  final double textSize=14;

  LoadingDialog(this.title); // 字体大小




  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => isCancel, // 拦截Android返回键
      child: GestureDetector(
        onTap: () {
          if (isCancel) {
// 点击Dialog外部关闭Dialog
            Navigator.pop(context);
          }
        },
        child: Container(
          padding: EdgeInsets.all(20), // 外边距
          color: Colors.transparent, // 必须设置颜色之后外层GestureDetector的onTap才会回调
          child: GestureDetector(
            onTap: () {}, // 点击Dialog不响应外部点击关闭事件
            child: Center(
              child: Material(
                color: color ?? Colors.white, // 背景
                borderRadius: borderRadius ?? BorderRadius.circular(2), // 倒角
                child: Padding(
                  padding: padding ?? EdgeInsets.fromLTRB(20, 15, 20, 15),
                  // 内边距
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: buildWidget(),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> buildWidget() {
    List<Widget> widgets = [];
    widgets.add(
        CupertinoActivityIndicator(animating: true, radius: indicatorRadius,));
    if (title != null && title.isNotEmpty && title
        .trim()
        .length > 0) {
      widgets.add(Flexible(
          child: Container(
            margin: EdgeInsets.only(top: 8),
            child: Text(
              title,
              style: TextStyle(
                color: textColor ?? Colors.black45,
                fontSize: textSize,
              ),
            ),
          )
      ));
    }
    return widgets;
  }





}
